package com.medicine.hbp.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.medicine.hbp.uitl.ToastUtil;
import com.medicine.hbp.R;

public class DetailEditDialog {

	private Activity activity;

	private Dialog dialog;

	private DetailEditDialogListener listener;
	public DetailEditDialog(Activity activity, DetailEditDialogListener listener) {
		this.activity = activity;
		this.listener = listener;
	}

	public void showDetailEditDialog() {
		View view = activity.getLayoutInflater().inflate(
				R.layout.detailedit_dialog, null);
		Button btn_fabu=(Button) view.findViewById(R.id.btn_fabu);
		final TextView txt_num=(TextView) view.findViewById(R.id.txt_num);
		final EditText edit_fabu=(EditText) view.findViewById(R.id.edit_fabu);
		dialog = new Dialog(activity, R.style.transparentFrameWindowStyle);
		dialog.setContentView(view, new LayoutParams(LayoutParams.MATCH_PARENT,
				LayoutParams.WRAP_CONTENT));
		Window window = dialog.getWindow();
		edit_fabu.addTextChangedListener(new TextWatcher() {
			@Override
			public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
			}

			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
										  int arg3) {
			}
			@Override
			public void afterTextChanged(Editable arg0) {
				txt_num.setText(arg0.length()+"");
			}
		});

		// 发布操作
		btn_fabu.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				String edit=edit_fabu.getText().toString();
				if(TextUtils.isEmpty(edit)){
					ToastUtil.showShort(activity, "您还没有填写评论");
					return;
				}
				InputMethodc();
				listener.onSelect(edit);
				dialog.dismiss();
			}
		});
		dialog.setOnDismissListener(new OnDismissListener() {
			@Override
			public void onDismiss(DialogInterface arg0) {
				InputMethodc();
			}
		});
		// 设置显示动画
		window.setWindowAnimations(R.style.show_photograpDialog_animstyle);
		WindowManager.LayoutParams wl = window.getAttributes();
		wl.x = 0;
		wl.y = activity.getWindowManager().getDefaultDisplay().getHeight();
		wl.width = ViewGroup.LayoutParams.MATCH_PARENT;
		wl.height = ViewGroup.LayoutParams.WRAP_CONTENT;
		dialog.onWindowAttributesChanged(wl);
		dialog.setCanceledOnTouchOutside(true);
		dialog.show();
		dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE |
				WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
	}
	public interface DetailEditDialogListener {
		public void onSelect(String content);
		public void onCancel();
	}
	/**
	 * 让软键盘消失
	 */
	private void InputMethodc() {
		View v = dialog.getWindow().getDecorView();
		// 得到InputMethodManager的实例
		InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
		// 如果开启
		if (imm.isActive()) {
			imm.hideSoftInputFromWindow(v.getWindowToken(),0);
			// 关闭软键盘，开启方法相同，这个方法是切换开启与关闭状态的
		}
	}
}